﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebTV_Kickstarter
{
    public partial class Goal
    {
        public static void AddGoalWithDbContainer(Goal goal, EFDataModelContainer eFDataModelContainer)
        {
            EFDataModelContainer db = null;

            if (eFDataModelContainer != null)
            {
                db = eFDataModelContainer;
                db.GoalSet.Add(goal);
                db.SaveChanges();
            }
        }

        public static List<string[]> FindGoalsForProjectWithDbContainer(Project project,
            EFDataModelContainer db)
        {

                IQueryable<Goal> goals = db.GoalSet;
                string[] titleArray =
                (
                    from g in goals
                    where g.Project.Id == project.Id
                    select g.Title
                    ).ToArray();

                int[] IdArray =
                (
                    from g in goals
                    where g.Project.Id == project.Id
                    select g.Id
                    ).ToArray();

                string[] descriptionArray =
                (
                    from g in goals
                    where g.Project.Id == project.Id
                    select g.Description
                    ).ToArray();

                int[] amountArray =
                    (
                        from g in goals
                        where g.Project.Id == project.Id
                        select g.Amount
                        ).ToArray();

            List<string[]> goalsList = new List<string[]>();
            for (int i = 0; i < titleArray.Count(); i++)
            {
                double percentage;
                if (amountArray[i] == 0)
                {
                    percentage = 0;
                }
                else
                {
                    percentage = Math.Round((( (double)project.TotalAmount / (double)amountArray[i] ) * (double)100),0);
                }
                
                string[] elements = {titleArray[i], descriptionArray[i], amountArray[i].ToString(), percentage.ToString(), (percentage*6).ToString(), IdArray[i].ToString() };
                goalsList.Add(elements);
            }
                return goalsList;
          
        }
    }
}